<?php

class Silk_Mapi_Controller_Action extends Mage_Core_Controller_Front_Action
{
	protected function render($data=array(), $msg='', $code = 200)
	{
		$result = array(
			'code' => $code,
			'msg' => $msg,
			'data' => $data
			);
		if($callback = $this->getRequest()->getParam('callback'))
			$data = $callback.'('.Zend_Json::encode($result).')';
        else
		    $data = Zend_Json::encode($result);

		$response = $this->getResponse();
		$response->setHeader('Content-type', 'application/json');
		$response->setBody($data);
	}

	protected function renderError($msg='', $code = 500)
	{
		$result = array(
			'code' => $code,
			'msg' => $msg,
			'data' => array()
			);

        if($callback = $this->getRequest()->getParam('callback'))
            $data = $callback.'('.Zend_Json::encode($result).')';
        else
            $data = Zend_Json::encode($result);

        $response = $this->getResponse();
		$response->setHeader('Content-type', 'application/json');
		$response->setBody($data);
	}

	public function getCustomer()
	{
		try
		{
			return Mage::getSingleton('customer/session')->getCustomer();
		}
		catch(Exception $e)
		{
			return Mage::getModel('customer/customer');
		}
	}
}